--伪表诚心
local weibiaochengxing = fk.CreateSkill {
    name = "mcdh_weibiaochengxing"
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

weibiaochengxing:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        return  (player:hasSkill(self.name) and player == target and target.phase == Player.Play) or
                (player == target and player:getMark("#weibiaochengxing_info-turn") ~= 0 and target.phase == Player.Finish)
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        if target.phase == Player.Play then
            local targets =room:askToChoosePlayers(player, {
                targets = room:getOtherPlayers(player),
                min_num = 1,
                max_num = 1,
                prompt = "#weibiaochengxing-choose",
                skill_name = self.name,
                cancelable = true
              })
            if #targets > 0  then
                event:setCostData(self,targets[1])
                return true
            end
        else
            return true
        end

    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if target.phase == Player.Play then
            local to = event:getCostData(self)
            local mark = player:getTableMark("#weibiaochengxing_info-turn")
            table.insertIfNeed(mark, to.id)
            room:setPlayerMark(player, "#weibiaochengxing_info-turn",mark)
            room:setPlayerMark(player, "@@weibiaochengxing_ban-turn", 1)
            room:setPlayerMark(to, "@@weibiaochengxing_ban-turn", 1)


            local targets = {player,to}
            local req = Request:new(targets, "AskForPoxi")
            req.focus_text = "mcdh_weibiaochengxing"
            for _, p in ipairs(targets) do
                local Requestdata = {
                    type = "mcdh_overt",
                    data = {{"$Hand",p:getCardIds("h")}},
                    extra_data = {1,999,"#weibiaochengxing-show","."},
                    cancelable = true
                }
                req:setData(p,  Requestdata)
                req:setDefaultReply(p,{})
            end

            local res = {}

            for _, p in ipairs(targets) do
                local replyCards = req:getResult(p)
                res[p.id] = replyCards
            end
            --FIXME 应该按当前回合角色排序，但是现在没写明置相关影响不大
            for _, p in ipairs(targets) do
                for _, id in ipairs(res[p.id]) do
                    local card = Fk:getCardById(id)
                    room:setCardMark(card,"@@mcdh_overt_card" , 1)
                end
            end
        else
            local mark = player:getTableMark( "#weibiaochengxing_info-turn")
            local moveInfos = {}
            table.insertIfNeed(mark,player.id)
            local num = 0
            for _, id in ipairs(mark) do
                local cards = {}
                for _, cid in ipairs(room:getPlayerById(id):getCardIds(Player.Hand)) do
                    if Fk:getCardById(cid):getMark("@@mcdh_overt_card") > 0 then
                        table.insertIfNeed(cards,cid)
                        num= num + 1
                    end
                end
                if #cards > 0 then
                    table.insert(moveInfos, {
                        from = id,
                        ids = cards,
                        toArea = Card.DiscardPile,
                        moveReason = fk.ReasonRecast ,
                        proposer = player,
                        skillName = self.name,
                        moveVisible = true
                    })
                    room:sendLog{
                        type = "#mcdh_Recast",
                        from = player.id,
                        to = {id},
                        card = cards,
                        arg = self.name,
                    }
                end
            end
            room:moveCards(table.unpack(moveInfos))
            player:drawCards(num,self.name)
        end
    end
})

weibiaochengxing:addEffect('prohibit',{
    prohibit_use = function(self, player, card)
        if player:getMark("@@weibiaochengxing_ban-turn") ~= 0 then
            local subcards = card:isVirtual() and card.subcards or {card.id}
            if #subcards > 0 then
                return  not table.every(subcards, function(id)
                    return Fk:getCardById(id):getMark("@@mcdh_overt_card") > 0
                end)
            else
                return true
            end
        end

    end,
    prohibit_response = function(self, player, card)
        if player:getMark("@@weibiaochengxing_ban-turn") ~= 0 then
            local subcards = card:isVirtual() and card.subcards or {card.id}
            if #subcards > 0 then
                return  not table.every(subcards, function(id)
                    return Fk:getCardById(id):getMark("@@mcdh_overt_card") > 0
                end)
            else
                return true
            end
        end
    end,
})

weibiaochengxing:addEffect('targetmod',{
    bypass_times = function(self, player, skill, scope, card, to)
        if card and to then
            local targetRecorded = player:getTableMark("#weibiaochengxing_info-turn")
            return table.contains(targetRecorded, to.id)
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_weibiaochengxing"] = "伪表诚心",
    [":mcdh_weibiaochengxing"] = "出牌阶段开始时，你可以选择一名其他角色，然后你与其可以同时明置任意张牌。本回合你与其仅能使用或打出明置牌，且本回合你对其使用牌无次数限制。回合结束阶段，你将双方所有的明置牌置入弃牌堆并摸等量牌。",
    ["#weibiaochengxing-choose"] = "伪表诚心:选择一名其他角色",
    ["#weibiaochengxing-show"] = "伪表诚心:展示任意张牌",
    ["@@weibiaochengxing_ban-turn"] = "伪表诚心",
    ["#mcdh_weibiaochengxing_prohibit"] = "伪表诚心"
}

return weibiaochengxing